//
//  NSArray+JCSafetyGetter.m
//  JCPublicKit-Objc
//
//  Created by JasonLee on 2020/4/24.
//

#import "NSArray+JCSafetyGetter.h"

@implementation NSArray (JCSafetyGetter)

- (NSObject * _Nullable)jc_objectAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return nil;
    }
    
    return [self objectAtIndex:index];
}

- (NSString * _Nullable)jc_stringAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return nil;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSString class]]) {
        return (NSString *)anyObject;
    } else if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@", (NSNumber *)anyObject];
    }
    
    return nil;
}

- (int)jc_intAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return 0;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject intValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject intValue];
    }
    
    return 0;
}

- (NSInteger)jc_integerAtIndex:(NSUInteger)index {
    return (NSInteger)[self jc_intAtIndex:index];
}

- (long long)jc_longLongAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return 0;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject longLongValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject longLongValue];
    }
    
    return 0;
}

- (double)jc_doubleAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return 0;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject doubleValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject doubleValue];
    }
    
    return 0;
}

- (NSDictionary * _Nullable)jc_dictionaryAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return nil;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSDictionary class]]) {
        return (NSDictionary *)anyObject;
    }
    
    return nil;
}

- (NSArray * _Nullable)jc_arrayAtIndex:(NSUInteger)index {
    if ([self jc_isIndexOutOfBound:index]) {
        return nil;
    }
    
    NSObject *anyObject = [self objectAtIndex:index];
    
    if ([anyObject isKindOfClass:[NSArray class]]) {
        return (NSArray *)anyObject;
    }
    
    return nil;
}

- (BOOL)jc_isIndexInBound:(NSInteger)index {
    if (index < 0) {
        return NO;
    }
    return (index < self.count);
}
- (BOOL)jc_isIndexOutOfBound:(NSInteger)index {
    if (index < 0) {
        return YES;
    }
    
    return !(index < self.count);
}

@end
